package co.edu.usbcali.raju.games.chess.model;

import java.util.ArrayList;
import java.util.List;

import co.edu.usbcali.raju.model.Cell;
import co.edu.usbcali.raju.model.Piece;
import co.edu.usbcali.raju.model.Position;

public class ChessCell implements Cell {
	
	List<ChessPiece> pieces=new ArrayList<ChessPiece>();
	ChessPosition position;
	
	public ChessCell() {
		super();
	}

	public ChessCell(int row,int col) {
		super();
		setPosition(row, col);
	}

	public ChessCell(int row,int col,ChessPiece piece) {
		super();
		setPosition(row, col);
		pieces.add(piece);
	}
	
	public ChessCell(ChessCell cell){
		super();
		setPosition(((ChessPosition)cell.getPosition()).row,((ChessPosition)cell.getPosition()).col);
		List<ChessPiece> nPieces=(List<ChessPiece>)cell.getPieces();
		for (ChessPiece piece : nPieces) {
			ChessPiece nPiece=new ChessPiece(piece, this);
			pieces.add(nPiece);
		}
	}

	/**
	 * Put specified piece into this cell, if there was other Piece this dissapear
	 */
	@Override
	public boolean addPiece(Piece piece) {
		pieces.clear();
		return pieces.add((ChessPiece)piece);
	}

	@Override
	public List<? extends Piece> getPieces() {
		return pieces;
	}

	@Override
	public Position getPosition() {
		return position;
	}

	@Override
	public boolean remove(Piece piece) {
		return pieces.remove(piece);
	}

	@Override
	public boolean samePosition(Cell cell) {
		return position.equals(cell.getPosition());
	}

	@Override
	public void setPosition(Position newPosition) {
		position=(ChessPosition)newPosition;
	}
	
	public void setPosition(int row,int col){
		position=new ChessPosition(row, col);
	}
	
	public void clear(){
		pieces.clear();
	}
	
	@Override
	public int getAbsolutePosition(){
		return ((ChessPosition)getPosition()).getAbsolutePosition();
	}

	@Override
	public int compareTo(Cell cell) {
		ChessCell other=(ChessCell)cell;
		return getPosition().compareTo(other.getPosition());
	}
	
	public String toString(){
		String ret=position.toString()+" ";
		for (ChessPiece piece : pieces) {
			ret+=piece+" ";
		}
		return ret;
	}

}
